<?php
define('OFFSET',3); // Record Per Page
//$url is already in layout
$url  = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'].'?page=show_all_users';
//pr($url); 
/**** START TO SORT ********/
$field = isset($_GET['field'])?$_GET['field'] : 'name';
$order = isset($_GET['order'])?$_GET['order'] : 'asc';
$toggleOrder = ($order == 'asc') ? 'desc' : 'asc';
/**** END SORT **/
/************ Start For Search ***********/
if(isset($_POST['searchTxt'])) {
	$search = $_POST['searchTxt'];
	$where = "WHERE `name` LIKE '%$search%'
OR `email` LIKE '%$search%'
OR `phone` LIKE '%$search%'";
} else {
	$where = '';
}
/************ End For Search ***********/

/************ Start For paging ***********/
$pageno = isset($_GET['pageno']) ? $_GET['pageno'] : 1;
//$limit = ($pageno * OFFSET )- OFFSET;
$limit = OFFSET *($pageno - 1);
/************ End For paging ***********/
  $selectCountQuery = "SELECT COUNT(*) AS `total` FROM `users` $where";

$selectCountResult = mysql_query($selectCountQuery) or die(mysql_error().__LINE__);
$ansCount = mysql_fetch_assoc($selectCountResult);
 $selectQuery = "SELECT * FROM `users` $where ORDER BY `$field` $order LIMIT $limit,".OFFSET;
$selectResult = mysql_query($selectQuery) or die(mysql_error().__LINE__);

$totalPages = ceil($ansCount['total'] / OFFSET );

?>


<div>Total Users found:<?=$ansCount['total']?></div>
<!-- SEarch form start here--->
    <form class="form-search" method="post">
		<input type="text" name="searchTxt" class="input-medium search-query">
		<input type="submit" class="btn btn-medium btn-success" value="Search "/>
    </form>
<!-- SEarch form end here--->    
    
<table border="1" width="100%" class="table table-striped table-hover">
  <thead>
    <tr>
      <th>#ID</th>
      <th>
        <a href="<?=$url?>?pageno=<?=$pageno?>&field=<?=$field?>&order=<?=$toggleOrder?>">Name</a>
      </th>
      <th>Email</th>
      <th>Phone</th>
      <th>image</th>
      <th>Register Date</th>
      <th>Options</th>
    </tr>
  </thead>
<?php
while($ans  = mysql_fetch_assoc($selectResult))
{
  $uid = $ans['id'];
  ?>
    <tr>
      <td>

<a target="_blank" href="layout.php?pageno=view_profile&id=<?=$ans['id']?>"><?=$uid?></a>
    </td>
      <td><?=$ans['name']?></td>
      <td><a href="mailto:<?=$ans['email']?>"><?=$ans['email']?></a></td>
      <td><?=$ans['phone']?></td>
      <td><img height="90" width="120" src="uploads/<?=$ans['image']?>" ></td>
       <td><?=date('d-M-Y l',strtotime($ans['register_date']))?></td>
      <td>
        <a target="_blank" href="layout.php?pageno=edit_user&id=<?=$uid?>"]><i class="icon-edit"></i></a>

        <a href="<?=$url?>?mode=delete&id=<?=$uid?>"]>
          <i class="icon-trash " onClick="return confirm('Are you sure to delete ?');"></i>
        </a>

      </td>
    </tr>
  <?php
}
?>
</table>
    <div class="pagination pagination-centered pagination-large">
		<ul>
			<li><a href="#">Prev</a></li>
<?php for($i=1 ; $i<= $totalPages ; $i++) {
$isActive = ($i == $pageno) ? 'active':'';
	
?>
			<li class="<?=$isActive?>">
<a href="<?=$url?>&pageno=<?=$i?>"><?=$i?></a></li>
<?}?>			

			<li>
<a href="<?=$url?>&pageno=<?=($pageno+1)?>">Next</a></li>
		</ul>
    </div>
